package org.jason.article.aop;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.jason.article.mq.producer.ArticleProducer;
import org.jason.common.constants.ArticleInteractionConstants;
import org.jason.model.interaction.dtos.ArticleInteractionDto;
import org.jason.model.article.es.ArticleContent;
import org.jason.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ArticleAop {

    @Autowired
    private ArticleProducer articleProducer;

    //文章阅读量增加
    @AfterReturning(value = "execution(* org.jason.article.controller.ArticleController.getArticleContent(..))"
            , returning = "result")
    public void IncreasedViewCount(ResponseResult<ArticleContent> result) {
        //如果文章获取成功
        if (result.getCode().equals(200)) {
            Integer articleId = result.getData().getId();
            //创建文章互动dto
            ArticleInteractionDto dto = new ArticleInteractionDto();
            dto.setArticleId(articleId);
            dto.setType(ArticleInteractionConstants.VIEWS);
            //发送消息到mq
            articleProducer.IncreasedViewCount(dto);
        }
    }
}
